﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyTienDien
{
    public partial class ThemKH : Form
    {
        private SqlConnection con;
        private DataTable dt = new DataTable("KhachHang");
        private DataTable dtbl = new DataTable("BienLai");
        private SqlDataAdapter da = new SqlDataAdapter();
        private Boolean kt;
        //ham ket noi
        private bool IsConnect()
        {
            bool isOpend = false;
            string cn = "Data Source=THANHHA;Initial Catalog=QUANLYTIENDIEN;Integrated Security=True";
            try
            {
                //MessageBox.Show("ket noi thanh cong");
                con = new SqlConnection(cn);
                con.Open();//mo ket noi
                if (con.State == ConnectionState.Open)
                {
                    isOpend = true;
                }
                else
                    isOpend = false;
            }
            catch (Exception)
            {
                MessageBox.Show("Không thể kết nối", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            return isOpend;
        }
        //ham dong ket noi
        private void disconnect()
        {
            con.Close();//dong ket noi
            con.Dispose();//giai phong tai nguyen
            con = null;//huy doi tuong

        }

        public ThemKH()
        {
            InitializeComponent();
        }
        private void HienThi()
        {
            SqlCommand com = new SqlCommand();//khai bao 1 command
            com.Connection = con;//ket noi
            com.CommandType = CommandType.Text;//khai bao kieu command
            com.CommandText = "SELECT * FROM KhachHang";
            da.SelectCommand = com;//gan command cho data
            da.Fill(dt);//nap dl len table
            com.CommandText = "SELECT * FROM BienLai";
            da.SelectCommand = com;//gan command cho data
            da.Fill(dtbl);//nap dl len table
        }

        private void formthemsv_Load(object sender, EventArgs e)
        {
            IsConnect();
            HienThi();
        }
        
        private bool kiemtra()
        {
            kt = true;
            if (txtmact.Text == "")
            {
                MessageBox.Show("Ban chua nhap ma cong to");
                kt = false;
            }
            return kt;
        }


        private void btnLuu_Click(object sender, EventArgs e)
        {
            kiemtra();
            if (kt == true)
            {
                int isInserted;
                if (IsConnect())
                {

                    string ma = txtmact.Text;
                    string hoten = txthoten.Text;
                    string sonha = txtsonha.Text;
                    SqlCommand comInsert = new SqlCommand();
                    comInsert.CommandText = @"INSERT INTO KhachHang
                    (MACONGTO,HOTEN,SONHA) VALUES(" + ma + ",N'" + hoten + "'," + sonha + ")";
                    comInsert.Connection = con;
                    comInsert.CommandType = CommandType.Text;
                    isInserted = comInsert.ExecuteNonQuery();//thuc thi truy van va khong can du lieu tra ve ap dung cho cau lenh insert, delete, update
                    if (isInserted > 0)
                    {

                        MessageBox.Show("Thêm thành công ");
                    }
                    else
                    {
                        MessageBox.Show("Chưa thêm được");
                    }
                }
                else
                {
                    MessageBox.Show("Không thể kết nối");
                }

                Close();
                disconnect();
                Dispose();
                
            }
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();//thoát
            Dispose();//giải phóng bộ nhớ
            MessageBox.Show("Bạn có chắc chắn thoát không?", "Thoát");
        }

    }
}
  
    

